package com.huadao.cart_ccs.data.bo;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.main.CartApplication;

/**
 * 蠕动泵
 */
public class PeristalticPumpBO extends BaseBO {
    private int speed;
    private byte direction;
    private byte alarm;

    public static final byte ModularType_PeristalticPump =0x1;
    public static final byte PeristalticPump_fluidIntake = 01;//进液蠕动泵
    public static final byte PeristalticPump_circulating = 02;//循环蠕动泵
    public static final byte PeristalticPump_waste = 03;//废液蠕动泵
    public static final byte PeristalticPump_reclaim = 04;//回收蠕动泵

    public static final byte stop= (byte) 0xAA;//停止
    public static final byte star= (byte) 0xCC;//启动
    public static final byte read= (byte) 0xFF;//读

    public static final byte directionReverse= (byte) 0x00;
    public static final byte directionForward= (byte) 0xFF;

    public static final byte alarm_normal=00000000;
    public static final byte alarm_error=00000001;
    public static final byte alarm_overflow_lowTension= (byte) (alarm_error<<1);
    public static final byte alarm_over_temperature=(byte) (alarm_error<<2);
    public static final byte alarm_headOpen=(byte) (alarm_error<<3);

    public PeristalticPumpBO() {
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public byte getDirection() {
        return direction;
    }

    public void setDirection(byte direction) {
        this.direction = direction;
    }

    public byte getAlarm() {
        return alarm;
    }

    public void setAlarm(byte alarm) {
        this.alarm = alarm;
    }

    @Override
    protected String getOrderStr() {
        String orderStr = null;
        if (order == star) {
            orderStr = CartApplication.getInstance().getString(R.string.star);
        } else if (order == stop) {
            orderStr = CartApplication.getInstance().getString(R.string.stop);
        } else if (order == read) {
            orderStr = CartApplication.getInstance().getString(R.string.read);
        }
        return orderStr;
    }

    @Override
    protected String getOrderContentStr() {
        if(order==stop){
            return super.getOrderContentStr();
        }

        String alarmStr = getAlarmStr();

        String directionStr = getDirectionStr(direction);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("转速:");
        stringBuilder.append(speed);
        stringBuilder.append("转向:");
        stringBuilder.append(directionStr);
        if (alarmStr != null) {
            stringBuilder.append("报警状态:");
            stringBuilder.append(alarmStr);
        }
        return stringBuilder.toString();
    }

    @Override
    protected String getModularTypeStr() {
        return CartApplication.getInstance().getString(R.string.peristaltic_pump);
    }

    @Override
    protected String getModularNOStr() {
        String modularNOStr=CartApplication.getInstance().getString(getModularNOStrByCode(modularNO));
//        if(modularNO== PeristalticPump_fluidIntake){
//            modularNOStr = CartApplication.getInstance().getString(R.string.peristaltic_pump_fluidIntake);
//        }else if(modularNO== PeristalticPump_circulating){
//            modularNOStr= CartApplication.getInstance().getString(R.string.peristaltic_pump_circulating);
//        }else if(modularNO== PeristalticPump_waste){
//            modularNOStr= CartApplication.getInstance().getString(R.string.peristaltic_pump_waste);
//        }else if(modularNO== PeristalticPump_reclaim){
//            modularNOStr= CartApplication.getInstance().getString(R.string.peristaltic_pump_reclaim);
//        }
        return modularNOStr;
    }

    public String getAlarmStr(){
        String alarmStr = null;
        if (alarm==alarm_normal) {
            alarmStr = CartApplication.getInstance().getString(R.string.alarm_normal);
        } else {
//            alarm= (byte) (alarm>>4);
            if((alarm>>4|0)==0){
                if ((alarm&alarm_error)==alarm_error) {
                    alarmStr = CartApplication.getInstance().getString(R.string.alarm_error);
                } else if ((alarm&alarm_overflow_lowTension)==alarm_overflow_lowTension) {
                    alarmStr = CartApplication.getInstance().getString(R.string.alarm_overflow_lowTension);
                } else if ((alarm&alarm_over_temperature)==alarm_over_temperature) {
                    alarmStr = CartApplication.getInstance().getString(R.string.alarm_over_temperature);
                } else if ((alarm&alarm_headOpen)==alarm_headOpen) {
                    alarmStr = CartApplication.getInstance().getString(R.string.alarm_headOpen);
                }
            }else {
                alarmStr = CartApplication.getInstance().getString(R.string.alarm_unknow);
            }
        }
        return alarmStr;
    }

    public static int getModularNOStrByCode(byte moduleNO) {
        if(moduleNO==PeristalticPumpBO.PeristalticPump_fluidIntake){
            return R.string.peristaltic_pump_fluidIntake;
        }else if(moduleNO==PeristalticPumpBO.PeristalticPump_circulating){
            return R.string.peristaltic_pump_circulating;
        }else if(moduleNO==PeristalticPumpBO.PeristalticPump_waste){
            return R.string.peristaltic_pump_waste;
        }else if(moduleNO==PeristalticPumpBO.PeristalticPump_reclaim){
            return R.string.peristaltic_pump_reclaim;
        }
        return R.string.unknow;
    }

    public static String getDirectionStr(byte direction){
        String directionStr = "";
        if (direction == directionForward) {
            directionStr = CartApplication.getInstance().getString(R.string.directionForward);
        } else if (direction == directionReverse) {
            directionStr = CartApplication.getInstance().getString(R.string.directionReverse);
        }else {
            directionStr = CartApplication.getInstance().getString(R.string.unknow);
        }
        return directionStr;
    }

}
